package com.xunk.common.core.utils;

import com.alibaba.fastjson.JSONObject;
import com.xunk.common.core.config.WeatherConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

/**
 * @ProjectName: HIM
 * @Package: com.xunk.common.core.utils
 * @ClassName: WeathUtil
 * @Author: tangguangyuan
 * @Description: 天气工具类
 * @Date: 2021/5/18 18:34
 * @Version: 1.0
 */
public class WeatherUtil {

    /**
     * 调用高德API获取地区天气信息
     * @param addressCode  地区code
     *
     * @return "province" -> "山东"
     *         "city" -> "崂山区"
     *         "adcode" -> "370212"
     *         "windpower" -> "≤3"
     *         "weather" -> "晴"
     *         "temperature" -> "21"
     *         "humidity" -> "42"
     *         "reporttime" -> "2021-05-18 18:31:17"
     *         "winddirection" -> "东南"
     */
    public static JSONObject queryWeatherFromAmap(Long addressCode){
        //添加获取天气地址参数
        String amapKey = WeatherConfig.getAmapKey();
        String amapUrl = WeatherConfig.getAmapUrl();

        StringBuilder url = new StringBuilder(amapUrl).append("?");
        url.append("key=").append(amapKey);
        url.append("&");
        url.append("city=").append(addressCode);
        //获取调度中心添加任务地址
        JSONObject response = HttpUtil.doGetstr(url.toString());
        String status = (String) response.get("status");
        if(StringUtils.isEmpty(status)||"0".equals(status) )
            throw new RuntimeException("获取天气信息失败");

        JSONObject lives= response.getJSONArray("lives").getJSONObject(0);
        return lives;
    }


}
